package com.example.viewtest.layout_9;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.example.viewtest.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * tab布局，每行宽度不够自动换行，居中对齐
 */
public class TabLayout extends ViewGroup {

    private List<Rect> childrenBounds;
    private static final float PADDING_TOP = Utils.db2px(3);
    private static final float PADDING_BOTTOM = Utils.db2px(3);

    public TabLayout(Context context) {
        this(context, null);
    }

    public TabLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int count = getChildCount();
        int lineWidth = MeasureSpec.getSize(widthMeasureSpec); //行宽
        int lineHeight = 0;  //行高
        int widthUsed = 0;
        int heightUsed = 0;
        int lineStartChildIndex = 0; //行子view起始索引
        if (childrenBounds == null) {
            initChildrenBounds(count);
        }
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            Rect rect = childrenBounds.get(i);

            measureChildWithMargins(child, widthMeasureSpec, widthUsed, heightMeasureSpec, heightUsed);
            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();
            if (lineWidth < childWidth + widthUsed) {
                amendChildRect(lineStartChildIndex, i, widthUsed, lineWidth, lineHeight);
                widthUsed = 0;
                heightUsed += lineHeight;
                measureChildWithMargins(child, widthMeasureSpec, widthUsed, heightMeasureSpec, heightUsed);
                childWidth = child.getMeasuredWidth();
                childHeight = child.getMeasuredHeight();
                lineHeight = 0;
                lineStartChildIndex = i;
            }
            rect.set(widthUsed, heightUsed, widthUsed + childWidth, heightUsed + childHeight);
            widthUsed += childWidth;
            lineHeight = Math.max(lineHeight, childHeight + (int) (PADDING_TOP + PADDING_BOTTOM));
        }
        amendChildRect(lineStartChildIndex, count, widthUsed, lineWidth, lineHeight);

        setMeasuredDimension(lineWidth, resolveSize(heightUsed + lineHeight, heightMeasureSpec));
    }

    //修复位置 居中
    private void amendChildRect(int startIndex, int endIndex, int widthUsed, int maxWidth, int maxHeight) {
        int emptyLength = (maxWidth - widthUsed) / (endIndex - startIndex + 1);
        int offsetX = 0;
        for (int i = startIndex; i < endIndex; i++) {
            Rect rect = childrenBounds.get(i);
            offsetX += emptyLength;
            int offsetY = (maxHeight - rect.height()) / 2;
            rect.offset(offsetX, offsetY);
        }
    }

    private void initChildrenBounds(int count) {
        childrenBounds = new ArrayList<>(count);
        for (int i = 0; i < count; i++) {
            childrenBounds.add(new Rect());
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            Rect rect = childrenBounds.get(i);
            child.layout(rect.left, rect.top, rect.right, rect.bottom);
        }
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }
}
